package com.wunu.smartlock1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.wunu.smartlock1.MainActivity;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.net.response.LoginResult;
import com.wunu.smartlock1.service.AccountService;


/**
 * 登录界面
 * Created by admin on 2018/4/21.
 */

public class LoginActivity_net_bak extends AppCompatActivity{

    Button login;
    Intent intent = new Intent();
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            System.out.println("msg----------------------->:"+msg);
            System.out.println("obj->"+msg.getData());
            LoginResult rs = (LoginResult)msg.obj;
            if(rs == null||rs.getData()==null||rs.getCode()!=200){
                Toast.makeText(LoginActivity_net_bak.this,"登录失败",Toast.LENGTH_SHORT).show();
                return;
            }
            System.out.println("obj:"+rs);

            Toast.makeText(LoginActivity_net_bak.this,"登录成功",Toast.LENGTH_SHORT).show();
            startActivity(intent.setClass(LoginActivity_net_bak.this, MainActivity.class));
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_lock_login);
        login = findViewById(R.id.loginButton);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(){
                    @Override
                    public void run(){
                        AccountService service = new AccountService();
                        try {
                            System.out.println("执行登录前:---------------");
                            LoginResult result = service.login("18018711017", "123123");
                            System.out.println("登录返回结果:"+result);

                            // 发送消息，把服务器返回的本文弹出吐司显示
                            Message msg = handler.obtainMessage();
                            msg.obj = result;
                            handler.sendMessage(msg);
                        }catch (Exception e){
                            System.out.println("登录异常:"+e);
                            System.out.println("登录异常信息:"+e);
                            Toast.makeText(LoginActivity_net_bak.this,"登录异常:"+e.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                };
                thread.start();
            }
        });

    }


}
